<?php
// +----------------------------------------------------------------------
// | snake
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2022 http://baiyf.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: NickBai <1902822973@qq.com>
// +----------------------------------------------------------------------
namespace app\admin\model;

use think\Model;

class AdminuserModel extends Model
{
    // 确定链接表名
    protected $name = 'users';
    
    /**
     * 根据搜索条件获取客户列表信息
     * @param $where
     * @param $offset
     * @param $limit
     */
    public function getUsersByWhere($where, $offset, $limit)
    {
        $data1 = $this->limit($offset, $limit)->order('id desc')->select();
        $RoleModel = new RoleModel;
        foreach ($data1 as $k => $v) {
            $roleNameInfo = $RoleModel->getOneRole($v['role_id']);
            $data1[$k]['role_name'] = $roleNameInfo['role_name'];
        }
        return $data1;
    }
    
    /**
     * 根据搜索条件获取所有的客户数量
     * @param $where
     */
    public function getAllUsers($where)
    {
        // var_dump( $this->where($where)->count());die;
        return $this->where($where)->count();
    }
    
    /**
     * 插入管理员信息
     * @param $param
     */
    public function insertUser($param)
    {
        try {
            $result = $this->validate('UserValidate')->save($param);
            if (false === $result) {
                // 验证失败 输出错误信息
                return msgs(-1, '', $this->getError());
            } else {
                return msgs(1, url('user/index'), '添加客户成功');
            }
        } catch (PDOException $e) {
            return msgs(-2, '', $e->getMessage());
        }
    }
    
    /**
     * 编辑管理员信息
     * @param $param
     */
    public function editUser($param)
    {
        try {
            $result = $this->validate('UserValidate')->save($param, ['id' => $param['id']]);
            if (false === $result) {
                // 验证失败 输出错误信息
                return msgs(-1, '', $this->getError());
            } else {
                return msgs(1, url('user/index'), '编辑客户成功');
            }
        } catch (PDOException $e) {
            return msgs(-2, '', $e->getMessage());
        }
    }
    
    /**
     * 根据管理员id获取角色信息
     * @param $id
     */
    public function getOneUser($id)
    {
        $where['_id'] = $id;
        return $this->where($where)->find();
    }
    
    /**
     * 删除管理员
     * @param $id
     */
    public function delUser($id)
    {
        try {
            $where['_id'] = $id;
            $this->where($where)->delete();
            return msgs(1, '', '删除管理员成功');
        } catch (PDOException $e) {
            return msgs(-1, '', $e->getMessage());
        }
    }
    
    /**
     * 根据客户名获取管理员信息
     * @param $name
     */
    public function findUserByName($name)
    {
        return $this->where('user_name', $name)->find();
    }
    
    /**
     * 更新管理员状态
     * @param array $param
     */
    public function updateStatus($param = [], $uid)
    {
        try {
            $this->where('_id', $uid)->update($param);
            return msgs(1, '', 'ok');
        } catch (\Exception $e) {
            return msgs(-1, '', $e->getMessage());
        }
    }
    
    /**
     * 根据客户名检测客户数据
     * @param $userName
     */
    public function checkUser($userName)
    {
        $data1 = $this->where('user_name', $userName)->find();
        $RoleModel = new RoleModel;
        $roleNameInfo = $RoleModel->getOneRole($data1['role_id']);
        $data1['role_name'] = $roleNameInfo['role_name'];
        $data1['rule'] = $roleNameInfo['rule'];
        return $data1;
    }
}
